package com.winqai.security.app;

import com.winqai.security.app.social.openid.OpenIdAuthenticationSecurityConfig;
import com.winqai.security.core.authentication.SmsCodeAuthenticationSecurityConfig;
import com.winqai.security.core.authorize.AuthorizeConfigManager;
import com.winqai.security.core.properties.SecurityConstants;
import com.winqai.security.core.properties.SecurityProperties;
import com.winqai.security.core.validate.code.ValidateCodeSecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;

@Configuration
@EnableResourceServer
public class WinqaiResourceServerConfig extends ResourceServerConfigurerAdapter{

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private AuthenticationFailureHandler winqaiAuthenticationFailureHandler;
    @Autowired
    private AuthenticationSuccessHandler winqaiAuthenticationSuccessHandler;

    @Autowired
    private ValidateCodeSecurityConfig validateCodeSecurityConfig;

    @Autowired
    private DataSource dataSource;


    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;

    @Autowired
    private OpenIdAuthenticationSecurityConfig openIdAuthenticationSecurityConfig;
    @Autowired
    private SpringSocialConfigurer winqaiSpringSocialConfigurer;

    @Autowired
    private AuthorizeConfigManager authorizeConfigManager;

    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
        //自动创建表
        //tokenRepository.setCreateTableOnStartup(true);
        return tokenRepository;
    }



    @Override
    public void configure(HttpSecurity http) throws Exception {

        http.formLogin()
                .loginPage(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL)
                .loginProcessingUrl(SecurityConstants.DEFAULT_LOGIN__PROCESSING_URL)
                .successHandler(winqaiAuthenticationSuccessHandler)
                .failureHandler(winqaiAuthenticationFailureHandler);

        http.apply(validateCodeSecurityConfig)
                .and()
                .apply(smsCodeAuthenticationSecurityConfig)
                .and()
                .apply(winqaiSpringSocialConfigurer)
                .and()
                .apply(openIdAuthenticationSecurityConfig)
                .and()
//                .rememberMe()
//                .tokenRepository(persistentTokenRepository())//数据源
//                .tokenValiditySeconds(securityProperties.getBrowser().getRememberMeSecods())//记住时间
//                .userDetailsService(myUserDetailsService)//到数据库获取用户信认证服务
//                .and()
//                .sessionManagement()
//                .invalidSessionStrategy(invalidSessionStrategy)
//                //.invalidSessionUrl(securityProperties.getBrowser().getSession().getSessionInvalidUrl())//session失效跳转地址
//                .maximumSessions(securityProperties.getBrowser().getSession().getMaximumSessions())
//                .maxSessionsPreventsLogin(securityProperties.getBrowser().getSession().isMaxSessionsPreventsLogin())//当session达到最大数值时阻止后来的登入行为
//                .expiredSessionStrategy(sessionInformationExpiredStrategy)
//                .and()
//                .and()
//                .logout()
//                .logoutUrl("/signOut")
//                .logoutSuccessHandler(logoutSuccessHandler)
//                .deleteCookies("JSESSIONID")
//                .and()
                .csrf().disable();
        //配置所有的权限配置的配置
        authorizeConfigManager.config(http.authorizeRequests());
    }
}
